<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/01/25 15:39:50 $)
// $RCSfile: index.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

define("SWIFT_AREA", 160);

if (defined("SWIFTCUSTOMPATH"))
{
	chdir(SWIFTCUSTOMPATH);
} else {
	chdir('./../');
}
require_once ("./swift.php");
require_once ("./includes/functions_pda.php");
$template->loadLanguageSection("pda");

if ($_REQUEST["_ca"] == "login") {
	/*
	* #########################################################
	* Authenticate User, Insert session and redirect else just display login form
	* #########################################################
	*/
	$staffid = $staffauth->authenticate($_POST["username"], $_POST["password"]);
	if (!empty($staffid))
	{
		// Authentication successfull, Generate a sessionid for this user
		$session->insertSession(SESSION_PDA, $staffid);

		if (!empty($_POST["querystring"]))
		{
			header("location: index.php?". iif(!stristr($_POST["querystring"], "sessionid"), "sessionid=". $_SWIFT["session"]["sessionid"] ."&").$_POST["querystring"]);
		} else {
			header("location: index.php?_m=tickets&_a=manage&sessionid=".$_SWIFT["session"]["sessionid"]);
		}
	} else {
		displayPDALoginForm($_SWIFT["errorstr"]);
	}
} else if ($_REQUEST["_ca"] == "logout") {
	$session->logoutSession(SESSION_PDA);
	$cookie->emptyCookie("staffsettings");

	displayPDALoginForm($_SWIFT["language"]["loggedout"]);
} else {
	if (!$session->start(SESSION_PDA)) {
		/*
		* #########################################################
		* No other action, we always try to start a session
		* #########################################################
		*/
		// Failed to load session
		displayPDALoginForm($_SWIFT["errorstr"]);
		exit;
	}

	// User logged in, trigger events
	if (!$events->triggerEvent(EVENT_PDA))
	{
		header("location: index.php?_m=tickets&_a=manage");
	}
}

$_SWIFT["endtime"] = fetchmicrotime();

?>